﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Claims;


namespace AIS.SharePoint.AspNetFBA.Code
{
    /// <summary>
    /// Handles claims & membership based operations for forms based authentication
    /// </summary>
    public class Claims
    {
        private SPIisSettings _SPIisSettings = null;
        /// <summary>
        /// INTERNAL: returns the SPIIsSetting for forms based authentication
        /// </summary>
        private  SPIisSettings SPIisSettings
        {
            get
            { 
                if (_SPIisSettings == null)
                {
                    _SPIisSettings = GetSPIisSettings();
                }
                return _SPIisSettings;
            }
        }

        /// <summary>
        /// Returns the identified forms membership provider for this web
        /// </summary>
        public string FormsMembersShipProviderName
        {
            get
            {
                return SPIisSettings.FormsClaimsAuthenticationProvider.MembershipProvider;
            }
        }

        /// <summary>
        /// Rerturns the membership provider for forms based authentication
        /// </summary>
        public System.Web.Security.MembershipProvider MembeshipProvider
        {
            get
            {
                return System.Web.Security.Membership.Providers[FormsMembersShipProviderName];
            }
        }

        /// <summary>
        /// Locates the current forms membership provider for the web
        /// </summary>
        /// <returns></returns>
        private SPIisSettings GetSPIisSettings()
        {
            SPIisSettings SPIIsSettings = null;
            foreach (SPUrlZone ZoneItem in Enum.GetValues(typeof(SPUrlZone)))
            {
                SPIisSettings IISSetting = null;
                SPContext.Current.Site.WebApplication.IisSettings.TryGetValue((SPUrlZone)ZoneItem, out IISSetting);
                if ((IISSetting != null) && (IISSetting.AuthenticationMode == System.Web.Configuration.AuthenticationMode.Forms))
                {
                    SPIIsSettings = IISSetting;
                    break;
                }
            }
            return SPIIsSettings;
        }

        /// <summary>
        /// Returns a forms based claim for a given username
        /// </summary>
        /// <param name="userName">User name to generate claim against</param>
        /// <returns>Claim</returns>
        public SPClaim GetClaimForUserName(string userName)
        {
            SPClaim NewClaim = new SPClaim(SPClaimTypes.UserLogonName
                , userName
                , "http://www.w3.org/2001/XMLSchema#string"
                , SPOriginalIssuers.Format(SPOriginalIssuerType.Forms, FormsMembersShipProviderName));
            return NewClaim;
        }
    }
}
